﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HexTileBoard
{
    public class SceneObject
    {
        #region PrivateVariables

        private Vector3 position = Vector3.Zero;
        private Vector3 rotation = Vector3.Zero;
        private float scale = 1.0f;
        private Matrix modelMatrix = Matrix.Identity;

        #endregion

        #region PublicVariables

        public Model ModelObject;
        public Texture2D[] ModelTextures;
        public Matrix[] Bones;

        public BoundingSphere BoundingSphere;

        #endregion

        #region Properties

        public Vector3 Position
        {
            get { return position; }
            set
            {
                position = value;
                modelMatrix = Matrix.CreateScale(scale) * Matrix.CreateRotationX(rotation.X) * Matrix.CreateRotationY(rotation.Y) * Matrix.CreateRotationZ(rotation.Z) * Matrix.CreateTranslation(position);
                BoundingSphere = BoundingSphere.Transform(modelMatrix);
            }
        }
        public Vector3 Rotation
        {
            get { return rotation; }
            set
            {
                rotation = value;
                modelMatrix = Matrix.CreateScale(scale) * Matrix.CreateRotationX(rotation.X) * Matrix.CreateRotationY(rotation.Y) * Matrix.CreateRotationZ(rotation.Z) * Matrix.CreateTranslation(position);
            }
        }
        public float Scale
        {
            get { return scale; }
            set
            {
                scale = value;
                modelMatrix = Matrix.CreateScale(scale) * Matrix.CreateRotationX(rotation.X) * Matrix.CreateRotationY(rotation.Y) * Matrix.CreateRotationZ(rotation.Z) * Matrix.CreateTranslation(position);
                BoundingSphere = BoundingSphere.Transform(modelMatrix);
            }
        }
        public Matrix ModelMatrix
        {
            get { return modelMatrix; }
            set { modelMatrix = value; }
        }

        #endregion

        #region Functions

        public SceneObject(Model p_Model, Texture2D[] p_ModelTextures, Vector3 p_Position, Vector3 p_Rotation, float p_Scale)
        {
            this.ModelObject = p_Model;
            this.ModelTextures = p_ModelTextures;

            Bones = new Matrix[this.ModelObject.Bones.Count];
            this.ModelObject.CopyAbsoluteBoneTransformsTo(Bones);

            this.Position = p_Position;
            this.Rotation = p_Rotation;
            this.Scale = p_Scale;
            this.ModelMatrix = Matrix.CreateScale(this.Scale) * Matrix.CreateRotationX(this.Rotation.X) * Matrix.CreateRotationY(this.Rotation.Y) * Matrix.CreateRotationZ(this.Rotation.Z) * Matrix.CreateTranslation(this.Position);

            BoundingSphere = BoundingSphere.Transform(modelMatrix);
        }

        public virtual void Draw(GameTime gameTime, SimpleCamera camera)
        {
        }

        public virtual void Update(GameTime gameTime)
        {
        }

        #endregion
    }
}
